import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 14785
 * Date: 2025-07-12
 * Time: 17:26
 */
public class test {

    public boolean threeConsecutiveOdds(int[] arr) {
        // 引入标志位记录连续出现奇数的个数
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (isConsecutiveOdd(arr[i])) {
                // 出现奇数，count ++;
                count ++;
                if (count == 3) {
                    // 出现连着三个奇数，返回true
                    return true;
                }
            }else {
                // 碰到偶数，count重置
                count = 0;
            }
        }
        return false;
    }

    private boolean isConsecutiveOdd(int num) {
        return num % 2 != 0;
    }
    public static boolean func2(int[] arr) {
        int start = 0;
        while (start < arr.length - 2) {
            if (arr[start] % 2 == 1) {
                if (arr[start + 1] % 2 == 1) {
                    if (arr[start + 2] % 2 == 1) {
                        return true;
                    } else start += 3;
                } else start += 2;
            } else start++;

        }
        return false;
    }

    public static void main8(String[] args) {
        int[] arr = {1, 2, 34, 3, 4, 5, 7, 23, 12};
        System.out.println(func2(arr));
    }

    /**
     * 排序法
     * 将数组排序后，数组n/2的元素一定是众数
     */
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length/2];
    }
    public static int func3(int[] arr) {
        int[] num = new int[10000000];
        for (int i = 0; i < arr.length; i++) {
            num[arr[i]]++;
            if (num[arr[i]] > arr.length / 2)
                return arr[i];
        }
        return -1;
    }

    public static void main7(String[] args) {
        int[] arr = {2, 2, 1, 1, 1, 2, 2,};
        System.out.println(func3(arr));
    }

    // 用异或运算的性质可以巧妙的解决这个问题，因为数组中只有一个数字出现一次
    // 则其他出现两次的数字用异或运算后都是0，最终整个数组异或运算的结果即为所求。
    public static int find1(int[]arr){//异或运算
        int ret = 0;
        for (int i : arr) {
            ret ^= i;
        }
        return ret;
    }
    public static int find(int[] arr) {
        int left = 0;
        int right = 1;
        Arrays.sort(arr);
        while (left <= arr.length - 1 && right <= arr.length - 1) {
            if (arr[left] != arr[right])
                return left;
            left += 2;
            right += 2;
        }
        return left;
    }

    public static void main6(String[] args) {
        int[] arr = {4, 1, 2, 1, 2};
        int ret = find1(arr);
        System.out.println(ret);
    }

    public static int[] func(int[] array, int target) {
        int[] arr = new int[2];

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (target == array[i] + array[j]) {
                    arr[0] = i;
                    arr[1] = j;
                }

            }
        }
        return arr;

    }

    public static void main5(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] ret = func(nums, target);
        System.out.println(Arrays.toString(ret));
    }

    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j - 1] > arr[j]) {
                    int tmp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = tmp;
                }
            }
        } // end for
    } // end bubbleSort

    public static void main4(String[] args) {
        int[] arr = {9, 5, 2, 7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static int binarySearch(int[] arr, int x) {
        int left = 0;
        int right = arr.length - 1;
        int mid = (left + right) / 2;
        while (left <= right) {
            if (x < arr[mid]) {
                right = mid - 1;
                mid = (left + right) / 2;
            } else if (x > arr[mid]) {
                left = mid + 1;
                mid = (left + right) / 2;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main3(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println(binarySearch(arr, 6));
    }

    public static void fun(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j < array.length - i; j++) {
                if (array[j - 1] % 2 == 0 && array[j] % 2 == 1) {
                    int tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        fun(array);
        System.out.println(Arrays.toString(array));
    }

    public static void transform(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }

    public static void main1(String[] args) {
        int[] array = {1, 2, 3};
        transform(array);
        System.out.println(Arrays.toString(array));
    }
}
